package nc.nc_xiuxian;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import static nc.nc_xiuxian.NC_XIUXIAN.plugin;
import static nc.nc_xiuxian.NC_XIUXIAN.ywxs;

public class COMMAND implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        switch (args.length){
            case 0:
            default:
                break;
            case 1:
                switch (args[0]){
                    case "chongxiu":
                        Player player = (Player) sender;
                        File file0 = new File(plugin.getDataFolder(),"sx");
                        if(!file0.exists()){
                            file0.mkdirs();
                        }
                        File file = new File(file0, player.getName()+".yml");
                        YamlConfiguration yml = YamlConfiguration.loadConfiguration(file);
                        if(!file.exists()){
                            try {
                                file.createNewFile();
                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            }
                        }
                        yml.set("gj",ywxs(Math.random() * 3.0));//攻击
                        yml.set("fy",ywxs(Math.random() * 3.0));//防御
                        yml.set("fs",ywxs(Math.random() * 3.0));//法伤
                        yml.set("sm",ywxs(Math.random() * 3.0));//生命值
                        yml.set("xy",ywxs(Math.random() * 3.0));//幸运
                        yml.set("sb",ywxs(Math.random() * 3.0));//闪避
                        yml.set("gf",1.0F);//功法
                        yml.set("maxmp",10.0F);//最大蓝条
                        yml.set("mp",10.0F);//蓝条
                        yml.set("maxjs",10.0F);//最大精神值
                        yml.set("js",10.0F);//精神值
                        yml.set("maxjq",1000.0F);//最大精力值
                        yml.set("jq",1000.0F);//精力值
                        yml.set("xw",0.0F);//修为值
                        yml.set("jj",0.0F);//境界
                        yml.set("xjj",0.0F);//小境界
                        try {
                            yml.save(file);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                        try {
                            LINGGENG.LGLOAD(player.getName());
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                        HashMap<String,Double> sx=new HashMap<>();
                        for(String s: yml.getKeys(false)){
                            sx.put(s,yml.getDouble(s));
                        }
                        plugin.playersx.put(player.getName(),sx); //属性读取到服务端
                        player.sendMessage("你废弃了自己的修为，准备重新修炼");
                        break;
                }
                break;
            case 3:
                switch (args[0]){
                    case "xwadd":
                        if(sender.isOp()){
                            Player player = Bukkit.getPlayer(args[1]);
                            double xw = Double.parseDouble(args[2]);
                            try {
                                SHUXING.sxsetadd(player,"xw",xw);
                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            }
                            sender.sendMessage("已为玩家"+player.getName()+"增加"+xw+"点修为\n"+"当前境界:"+plugin.playerjj.get(player.getName()));
                        }
                        break;
                    case "jsadd":
                        if(sender.isOp()){
                            Player player = Bukkit.getPlayer(args[1]);
                            double js = Double.parseDouble(args[2]);
                            try {
                                SHUXING.sxsetadd(player,"js",js);
                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            }
                            sender.sendMessage("已为玩家"+player.getName()+"增加"+js+"点精神");
                        }
                        break;
                    case "jqadd":
                        if(sender.isOp()){
                            Player player = Bukkit.getPlayer(args[1]);
                            double jq = Double.parseDouble(args[2]);
                            try {
                                SHUXING.sxsetadd(player,"jq",jq);
                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            }
                            sender.sendMessage("已为玩家"+player.getName()+"增加"+jq+"点精力");
                        }
                        break;
                    case "maxjsadd":
                        if(sender.isOp()){
                            Player player = Bukkit.getPlayer(args[1]);
                            double js = Double.parseDouble(args[2]);
                            try {
                                SHUXING.sxsetadd(player,"maxjs",js);
                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            }
                            sender.sendMessage("已为玩家"+player.getName()+"增加"+js+"点精神");
                        }
                        break;
                    case "maxjqadd":
                        if(sender.isOp()){
                            Player player = Bukkit.getPlayer(args[1]);
                            double jq = Double.parseDouble(args[2]);
                            try {
                                SHUXING.sxsetadd(player,"maxjq",jq);
                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            }
                            sender.sendMessage("已为玩家"+player.getName()+"增加"+jq+"点精力");
                        }
                        break;
                }
                break;
        }
        return false;
    }
}
